﻿(function() {
    'use strict';
    angular.module('app').factory('common', ['$rootScope','$http','$q','$location','config',
        function ($rootScope,$http, $q, $location, config) {
            return {
                config: config,
                activateController: activateController,
                confirm: confirm,
                moment: moment,
                redirectToUrl: redirectToUrl
            };

            function activateController(promises) {
                return $q.all(promises).then(function() {
                    $rootScope.$broadcast(config.events.activateController);
                });
            }

            function confirm(text,callBack) {
                bootbox.confirm(text, callBack);
            }

            function redirectToUrl(scope,url) {
                if (scope) {
                    scope.$apply($location.path(url));
                } else {
                    $location.path(url);
                }
                $location.url($location.path());
            }

        }]);
    angular.module('app').factory('httpInterceptor', ['$q', '$rootScope',
        function ($q, $rootScope) {
            var numLoadings = 0;
            return {
                request: function (config) {
                    numLoadings++;
                    // Show loader
                    $rootScope.$broadcast("loader_show");
                    return config || $q.when(config);
                },
                response: function (response) {

                    if ((--numLoadings) === 0) {
                        // Hide loader
                        $rootScope.$broadcast("loader_hide");
                    }
                    return response || $q.when(response);
                },
                responseError: function (response) {
                    if (!(--numLoadings)) {
                        // Hide loader
                        $rootScope.$broadcast("loader_hide");
                    }
                    return $q.reject(response);
                }
            };
        }]);
    angular.module('app').service('uploadService',[
        function (uploadServiceUrl) {
            var initializeFn, processFn;
            initializeFn = function (e, data, process) {
                var upload;
                return upload = {
                    message: '',
                    uploadurl: 'http://localhost:22362/api/Image/Upload',
                    status: false
                };
            };
            processFn = function (e, data, process) {
                var file, upload;
                upload = {};
                upload.successData = [];
                upload.status = true;
                upload.error = false;
                if (process === 'done') {
                    upload.message = 'Uploaded Succesfully!!!';
                    file = data.result.files[0];
                    upload.successData = {
                        name: file.name,
                        fullUrl: file.url,
                        thumbUrl: file.thumbnail_url
                    };
                } else if (process === 'progress') {
                    upload.message = 'Uploading....!!!';
                } else {
                    upload.error = true;
                    upload.message = 'Uploaded Failed!!! Booo!!! :( (Check console)';
                    console.log(e, data);
                }
                return upload;
            };
            return {
                process: processFn,
                initialize: initializeFn
            };
    }]);
})();